
/**
 * MainDo
 * 功能：
 * 基本的类型转换
 * 从数据库获取基本数据
 * author：shinion
 */

/**
 * 数据操作类，
 * 进行各类数据操作
 */
MainDo = {};
MainDo.debug = true;
MainDo.childMenuDefaultStr = "默认";
MainDo.menuId = 'm_menu_';
MainDo.mouseOverColor = 'rgb(243,243,243)';


//根据icon,获得图片
MainDo.getIcon = function(iconName){
	var defaultImgPath= globalConfig.webPath + "/image/default.png";
	var str = iconName.split((/[-]/));
	var returnImgPath;
	if(str[1]){
		//解析正确
		returnImgPath = globalConfig.webPath + "/image/" +str[1] + ".png";
		return returnImgPath;
	}else{
		//解析icon格式错误，返回默认图片
		return defaultImgPath;
	}
};

/**
 * 根据功能name，获取相应的图片
 */
MainDo.getImg = function(name){
	var path = globalConfig.webPath + "/image2/";
	var returnImgPath;
	switch(name){
		case"实时监控":
		case"车辆监控":
		returnImgPath = path + "onlinecar1.png?v=850302";
		break;

		case"报警处理":
		returnImgPath = path + "alarm1.png?v=8503021";
		break;

		case"部标视频":
		case"实时视频":
		returnImgPath = path + "realVideo.png?v=850302";
		break;

		case"历史视频":
		returnImgPath = path + "history.png?v=850302";
		break;

		case"报表服务":
		case"报表管理":
		case"统计报表":
		returnImgPath = path +  "report1.png?v=850302";
		break;

		case"基础资料":
		case"运营管理":
		returnImgPath = path + "editInfo1.png?v=850302";
		break;

		case"用户管理":
		returnImgPath = path + "user1.png?v=850302";
		break;

		case"系统管理":
		case"系统设置":
		returnImgPath = path + "config1.png?v=850302";
		break;

		case"退出":
		returnImgPath = path + "exit1.png?v=850302";
		break;

		default:
		returnImgPath = path + "fun-default.png?v=850302";
		break;
	}
	return returnImgPath;

};


MainDo.leftInit = function(data){
	var tt = $('#treeTab'); //如果tab已经存在,则选中并刷新该tab   
	if(tt != undefined){
		tt.tabs('select', "功能面板");
	}       

	//更改面板title，暂无效
	// var leftMenus = $("#leftMenus");
	// if((leftMenus != undefined) && data){
	// 	leftMenus.attr("title",data.text);
	// }//end change

	var leftUp = $("#leftUp");
	var leftDown = $("#leftDown");
	if(leftUp != undefined){
		leftUp.html("");
		$('#vehicleTree').hideLoading(); 
	}else{
		return false;
	}
	if(leftDown != undefined){
		//leftDown.css("display","none");
		leftDown.hide();
	}else{
		return false;
	}	
	return true;
};


///////////////////////////////////////////////////////新增加的折叠面板菜单,author:shinion date:2019-10-17
/**
 * 根据数据，初始化左侧菜单信息
 * 
 */
MainDo.initAccordDiv = function(id,data){
	let leftUp = $("#" + id);
	// this.outDebug("leftUp： " , leftUp);
	if(leftUp != undefined && leftUp != null){
		//解析菜单数据
		let keyList = [];
		let menuMap = {};
		if(!DataUtil.isDefined(data.items) && data.items.length > 0){
			this.formatMenuDatas(data,keyList,menuMap);	
		}
		// DataUtil.outDebug('解析后的菜单数据：' , menuMap);
		this.showMenuDatas(id,keyList,menuMap);
			
	}
}
//解析菜单数据
MainDo.formatMenuDatas = function(data,keyList,menuMap){
	try {
		$.each(data.items,function(n,item){
			//获取key值
			let key = '';
			if(!DataUtil.isDefined(item.childMenu) && item.childMenu != ""){
				key = item.childMenu;
			}else{
				key = MainDo.childMenuDefaultStr;
			}
			//根据key值获取对象列表
			if(key != ''){
				if(key != MainDo.childMenuDefaultStr && keyList.indexOf(key) < 0){
					keyList.push(key);
				}
				let obj = menuMap[key];
				if(DataUtil.isDefined(obj)){
					obj = {
						title:key,//折叠面板标题，子菜单名称
						list:[],//子菜单列表
					};
					menuMap[key] = obj;
					obj.list.push(item);
				}else{
					obj.list.push(item);
				}
			}
		});
		keyList.push(MainDo.childMenuDefaultStr);
	} catch (error) {
		DataUtil.outError("解析菜单数据失败：" + error);
	}
}
//显示菜单数据
MainDo.showMenuDatas = function (id,keyList, menuMap) {
	let leftUp = $("#" + id);
	//显示菜单
	let html = '';
	html += '<div class="easyui-accordion" style="width:100%;">'//折叠面容器
	$.each(keyList, function (n, key) {
		let obj = menuMap[key];
		if (!DataUtil.isDefined(obj)) {
			let str = '';
			// MainDo.outDebug("当前子菜单：" + key + " ， 子菜单内容：" ,obj.list);
			$.each(obj.list, function (m, item) {
				//报警查询处理使用红色
				let textStr = '<span>' + item.text + '</span>';
				if(item.text == "报警处理查询"){
					textStr = '<span style="color:red;">' + item.text + '</span>';
				}
				str += '<div id="' + (MainDo.menuId + item.id) + '" onmouseover="javascript:MainDo.leftMouseOver(this);"'
					+ ' onmouseout="javascript:MainDo.leftMouseOut(this);" '
					+ 'onclick="javascript:MainDo.leftClick(&quot;' + item.text +'&quot;,&quot;' + item.url + '&quot;,&quot;' + item.icon + '&quot;);">'
					+ '<img src="' + MainDo.getIcon(item.icon) + '">'
					+ '&nbsp;'
					+ textStr
					+'</div>';
			});
			if(str != ''){
				//报警查询处理的折叠块默认展开,折叠块标题为:报警综合查询
				let openStr = '';
				if(obj.title == '报警综合查询'){
					openStr = ' selected="true" ';
				}
				html += '<div title="' + obj.title + '" class="accord_child_div_css"  style="overflow:auto;padding:10px;" ' + openStr + '>'//折叠面板
				html += str;
				html += '</div>';
			}
		}
	});
	html += '</div>';
	leftUp.html(html);
	//初始化折叠面板
	$(".easyui-accordion").accordion();
}


/**
* 左侧功能面板点击事件
*/
function leftMenuClick(evt) {
	if (evt) {
		data = evt.data;
		console.log(data.url);
		switch (data.text) {
			case "百度地图":
				var mapType = "baidu";
				MyMenu.setMapPath(mapType);
				break;
			case "高德地图":
				var mapType = "amap";
				MyMenu.setMapPath(mapType);
				break;
			case "四维地图":
				var mapType = "smart";
				MyMenu.setMapPath(mapType);
				break;
			default:
				addTab(data.text, data.url, data.iconCls);
				break;

		}
	}
	// var left = $("#leftMenus");
}

//鼠标点击事件
MainDo.leftClick = function(text,url,icon){
	switch (text) {
		case "百度地图":
			var mapType = "baidu";
			MyMenu.setMapPath(mapType);
			break;
		case "高德地图":
			var mapType = "amap";
			MyMenu.setMapPath(mapType);
			break;
		case "四维地图":
			var mapType = "smart";
			MyMenu.setMapPath(mapType);
			break;
		case "报警明细查询":
			$('#mainTab').tabs('close', text);
			addTab(text, url,icon);
			break;
		default:
			addTab(text, url,icon);
			break;

	}

}
//鼠标移动到元素上事件
MainDo.leftMouseOver = function(evt){
	// MainDo.outDebug("移动事件：" ,evt);
	var left =$("#" + evt.id);
	if(left != undefined){
		//left.css("cursor","pointer");
		left.css("background",MainDo.mouseOverColor);
	}      									
}

//鼠标从元素上移开事件
MainDo.leftMouseOut = function(evt){
	var left =$("#" + evt.id);
	if(left != undefined){
		left.css("background","");
		//left.css("cursor","default");
	}
}


//输出调试信息
MainDo.outDebug = function(msg,data){
	if(this.debug == true){
		DataUtil.outDebug(msg,data);
	}
}





//绑定事件
function leftAddEvt(data){
	if(data != undefined){
		var left = $("#" + data.id);
		if(left != undefined){
			//console.log("leftAddEvt->"+data.id + " ,"+ data.text);
			left.css("cursor","pointer");
			left.bind("click", data, leftMenuClick);
			left.bind("mouseover",data.id,leftMouseOver);
			left.bind("mouseout",data.id,leftMouseOut);
		}
	}
}


